/**
 * Project Name:fastPlat
 * File Name:FeedbackController.java
 * Package Name:com.fast.cms.web.controller.article
 * Date:2017年05月27日 17时48分32秒
 * Copyright (c) 2017, lsflu@126.com All Rights Reserved.
 *
*/

package com.fast.cms.web.controller.article;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fast.base.db.page.PageView;
import com.fast.base.db.sql.AbstractSqlGenerator;
import com.fast.base.model.AnoSysLog;
import com.fast.base.util.LogUtils;
import com.fast.base.util.StringUtils;
import com.fast.base.util.web.ResponseData;
import com.fast.base.util.web.ResponseEnum;
import com.fast.base.web.BaseController;
import com.fast.cms.model.article.Feedback;
import com.fast.cms.service.article.FeedbackService;
import com.fast.cms.web.form.FeedbackForm;
import com.fast.common.userCache.UserRequestUtils;

/**
 * ClassName:FeedbackController <br/>
 * 用途: 意见反馈 web控制类 <br/>
 * 开发人员：lushuifa  http://lushuifa.iteye.com/
 * 邮箱:lsflu@126.com
 * @version  1.0
 * @since    JDK 1.6
 * @see
 * Date:     2017年05月27日 17时48分32秒 <br/> 	 
 */
@Controller
@RequestMapping("/feedback")
public class FeedbackController extends BaseController{
	
	
	@Resource(name="feedbackService")
	private FeedbackService feedbackService;

	/**
	 * 编辑意见反馈信息
	 * @param imageUrl
	 * @return
	 */
	@RequestMapping("/editFeedback.do")
	@ResponseBody
	@AnoSysLog(actionName="编辑意见反馈信息")
	public ResponseData editFeedback(){
		ResponseData responseData = new ResponseData();
		Feedback feedback = null;
		try {
			int id = webUtil.getInt("id");
			if(id>0){
				feedback = this.feedbackService.getById(Feedback.class,id);
				feedback = (Feedback) webUtil.bindDataToBean(feedback);
			}else{
				feedback = new Feedback();
				feedback = (Feedback) webUtil.bindDataToBean(feedback);
				UserRequestUtils.bindCreaterInfo(feedback);
			}
			UserRequestUtils.bindEditorInfo(feedback);
			if(this.feedbackService.merge(feedbackService)!=null){
				responseData.buildResult(ResponseEnum.EDIT_SUCCESS);
			}else{
				responseData.buildResult(ResponseEnum.EDIT_FAILUE);
			}
		} catch (Exception e) {
			e.printStackTrace();
			responseData.failue();
		}
		return responseData;
	}
	
	/**
	 * 删除意见反馈信息到回收站
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/delFeedbackByIdForHS.do")
	@AnoSysLog(actionName="删除意见反馈到回收站")
	public ResponseData delFeedbackByIdForHS(int id) {
		LogUtils.info("delFeedbackByIdForHS id=:"+id);
		ResponseData responseData = new ResponseData();
		try {
			String sql = "update "+AbstractSqlGenerator.getTableName(Feedback.class)+" set delete_Flag=1 where id="+id;
			boolean actionFlag = feedbackService.modifyRow(sql)>0;
			responseData.setCode(actionFlag?"00":"01");
			if(actionFlag){
				responseData.buildResult(ResponseEnum.DELETE_SUCCESS);
			}else{
				responseData.buildResult(ResponseEnum.DELETE_FAILUE);
			}
		} catch (Exception e) {
			e.printStackTrace();
			responseData.failue();
		}
		LogUtils.info("delFeedbackByIdForHS responseData=:"+responseData.toString());
		return responseData;
	}

	/**
	 * 删除意见反馈信息
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/delFeedbackById.do")
	@AnoSysLog(actionName="彻底删除意见反馈")
	public ResponseData delFeedbackById(int id) {
		LogUtils.info("delFeedbackById id=:"+id);
		ResponseData responseData = new ResponseData();
		try {
			boolean actionFlag = feedbackService.deleteById(Feedback.class, id);
			responseData.setCode(actionFlag?"00":"01");
			if(actionFlag){
				responseData.buildResult(ResponseEnum.DELETE_SUCCESS);
			}else{
				responseData.buildResult(ResponseEnum.DELETE_FAILUE);
			}
		} catch (Exception e) {
			e.printStackTrace();
			responseData.failue();
		}
		LogUtils.info("delFeedbackById responseData=:"+responseData.toString());
		return responseData;
	}
	
	/**
	 * 删除多个意见反馈信息
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/batchDelFeedback.do")
	@AnoSysLog(actionName="批量删除意见反馈评论")
	public ResponseData batchDelFeedback(String ids){
		ResponseData responseData = new ResponseData();
		try {
			int actionCount = feedbackService.deleteByIds(Feedback.class, ids);
			if(actionCount>0){
				responseData.buildResult(ResponseEnum.DELETE_SUCCESS);
			}else{
				responseData.buildResult(ResponseEnum.DELETE_FAILUE);
			}
		} catch (Exception e) {
			e.printStackTrace();
			responseData.failue();
		}
		return responseData;
	}
	
	/**
	 * 分页得到意见反馈列表
	 * @return PageView<Feedback>
	 * @throws IOException 
	 */
	@ResponseBody
	@RequestMapping("/getFeedbackList.do")
	public Object getFeedbackList(FeedbackForm feedbackForm){
		LogUtils.info("getFeedbackList param:"+feedbackForm.toString());
		Map<String,Object> gridMap = new HashMap<String,Object>();
		PageView<Feedback> pageView = null;
		try {
			pageView = this.feedbackService.getFeedbackList(feedbackForm);
			gridMap.put("total", pageView.getTotalRecord());
			gridMap.put("rows", pageView.getRecords());
		} catch (Exception e) {
			LogUtils.error("getFeedbackList err:",e);
		}
		LogUtils.info("getFeedbackList result:"+pageView.toString());
 		return gridMap;
	}
	
	/**
	 * 查询意见反馈详情
	 * @param id
	 * @return
	 */
	@RequestMapping("/getFeedbackDetail.do")
	public ModelAndView getFeedbackDetail(int id,String actionType) {
		ModelAndView mv = new ModelAndView();
		Feedback bean = feedbackService.queryForBeanById(Feedback.class, id);
		try {
			mv.addObject("feedback",bean);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(StringUtils.equals(actionType,"update")){
			mv.setViewName("/adminMain/feedback/feedback-add.jsp");
		}else{
			mv.setViewName("/adminMain/feedback/feedback-detail.jsp");
		}
		return mv;
	}
}

